import Vue from 'vue'
import Vuex from 'vuex'
import $H from '@/common/request.js';
import $C from '../common/config.js';
import io from '../common/uni-socket.io.js';
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		user: '',
		token: '',
		socket: null

	},
	actions: {
		// 断开socket连接
		closeSocket({
			state,
			dispatch
		}) {
			if (state.socket) {
				state.socket.close()
			}
		},
		// 连接socket
		connectSocket({
			state,
			dispatch
		}) {
			const S = io($C.socketUrl, {
				query: {},
				transports: ['websocket'],
				timeout: 5000
			})
			// 实时监听在线消息
			let onlineEvent = (e) => {
				uni.$emit('live', {
					type: 'online',
					data: e
				})
			}
			// 实时监听在线评论
			let commentEvent = (e) => {
				uni.$emit('live', {
					type: 'comment',
					data: e
				})
			}
			// 实时监听在线礼物
			let giftEvent = (e) => {
				uni.$emit('live', {
					type: 'gift',
					data: e
				})
			}
			// 监听连接
			S.on('connect', () => {
				console.log('已连接')
				state.socket = S
				// socket.i0 唯一链接id,可以监控这个ID实现点对点监控
				let {
					id
				} = S
				S.on(id, (e) => {
					console.log(e);
					let d = e.data

					if (d.action === 'error') {
						let msg = d.payload
						if (msg === 'Token令牌不合法') {
							// 退出登录操作
							return dispatch('logout')
						}
						if (e.meta.notoast) {
							return
						}
						uni.showToast({
							title: msg,
							icon: 'none'
						});
					}
				})
				S.on('online', onlineEvent)
				S.on('comment', commentEvent)
				S.on('gift', giftEvent)
			})
			// 移除监听时间
			const removeListener = () => {
				if (S) {
					S.removeListener('online')
					S.removeListener('comment')
					S.removeListener('gift')
				}
			}
			// 监听失败
			S.on('error', () => {
				state.socket = null
				console.log('连接失败')
			})
			// 监听断开
			S.on('disconnect', () => {
				state.socket = null
				console.log('已断开')
			})
		},
		authMethod({
			state
		}, callback) {
			if (!state.token) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				return uni.navigateTo({
					url: '/pages/login/login'
				})
			}
			callback()
		},
		// 初始化登录
		login({
			state
		}, user) {
			state.user = user
			state.token = user.token
			uni.setStorageSync('user', JSON.stringify(user))
			uni.setStorageSync('token', user.token)
		},
		// 初始化用户信息
		initUser({
			state,
			dispatch
		}) {
			let user = uni.getStorageSync('user')
			let token = uni.getStorageSync('token')
			if (user) {
				user = JSON.parse(user)
				state.user = user
				state.token = token
				dispatch('connectSocket')
			}
		},
		// 退出登录
		logout(state) {
			$H.post('/logout', {}, {
				token: true
			})
			dispatch('closeSocket')
			uni.removeStorageSync('user')
			uni.removeStorageSync('token')
			state.user = null
			state.token = null
			uni.reLaunch({
				url: '/pages/login/login'
			})
		},
		// getuserinfo
		getUserInfo({
			state
		}) {
			$H.get('/user/info', {
				token: true,
				noJump: true,
				toast: false
			}).then(res => {
				state.user = res
				uni.setStorageSync('user', JSON.stringify(user))
			})
		}

	}
})
